using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.City;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Hanging
{    
    public class HangingPveLevelManager : MonoBehaviour
    {     
        [Header("资源配置")]
        // 场景资源文件
        [SerializeField]
        private string _SceneAssetPath = "ResourcesAssets/HangingPveLevel/Prefabs/Scene/rpg_tuitu_scene.prefab";
        // 玩家资源路径
        [SerializeField]
        private string _PlayerAssetPath = "ResourcesAssets/HangingPveLevel/Prefabs/Controller/PlayerController.prefab";
        // npc资源路径
        [SerializeField]
        private List<string> _NpcAssetPaths = new List<string>
        {
            "ResourcesAssets/HangingPveLevel/Prefabs/Npc/mesh_npc03_M.prefab",
            "ResourcesAssets/HangingPveLevel/Prefabs/Npc/mesh_SOLD411022_Smg_M.prefab",
        };

        [Header("参数配置")]
        // 场景初始化偏移
        [SerializeField]
        private float _InitSceneOffset = 90f;
        // 场景长度
        [SerializeField]
        private float _SceneWidth = 192.5f;
        
        
        // 当前资源加载器
        private List<ResLoader> _ResLoaders = new List<ResLoader>();
        // 当前场景资源
        private GameObject _SceneAsset;
        // 当前玩家资源
        private GameObject _PlayerAsset;
        
        // 场景GameObject
        private GameObject _SceneGameObject;
        private GameObject _SceneGameObject2; // 这块用来拼接，防止穿帮
        
        // 玩家GameObject
        private GameObject _PlayerGameObject;
        
        public void EnterScene(Action callback)
        {
            bool loadSceneComplete = false;
            bool loadPlayerComplete = false;

            // 加载场景资源
            loadResource(_SceneAssetPath, asset =>
            {
                _SceneAsset = asset as GameObject;

                loadSceneComplete = true;
                if (loadSceneComplete && loadPlayerComplete)
                {
                    StartCoroutine(constructSceneCoroutine());
                }
            });
            
            // 加载玩家资源
            loadResource(_PlayerAssetPath, asset =>
            {
                _PlayerAsset = asset as GameObject;

                loadPlayerComplete = true;
                if (loadSceneComplete && loadPlayerComplete)
                {
                    StartCoroutine(constructSceneCoroutine());
                }
            });
            
        }

        IEnumerator constructSceneCoroutine()
        {
            yield return null;
            _SceneGameObject = Instantiate(_SceneAsset);
            yield return null;
            _SceneGameObject2 = Instantiate(_SceneAsset);
            
        }

        // 加载资源
        void loadResource(string path, Action<object> callback)
        {
            ResLoader resLoader = ResLoader.Alloc();
            resLoader.Add2Load(path, (success, name, asset) => {
                if (success)
                {
                    callback?.Invoke(asset);
                }
                else
                {
                    Debug.LogError("load Asset failed : {0} " + name);
                }
            });
            resLoader.Load();
            
            _ResLoaders.Add(resLoader);
        }
        
    }
}